"""Lead contact model for CRM-SaaS system."""

from __future__ import annotations

import enum
from typing import TYPE_CHECKING

from sqlalchemy import ForeignKey, String, Enum as SQLEnum
from sqlalchemy.orm import Mapped, mapped_column, relationship

from ..db.base_class import Base

if TYPE_CHECKING:
    from ..models.lead import Lead


class PreferredChannel(str, enum.Enum):
    """Preferred communication channels."""

    WECHAT = "wechat"
    PHONE = "phone"
    EMAIL = "email"
    OTHER = "other"


class LeadContact(Base):
    """Lead contact person."""

    __tablename__ = "leadcontact"
    id: Mapped[int] = mapped_column(primary_key=True, index=True)
    name: Mapped[str] = mapped_column(String(100), nullable=False)
    email: Mapped[str | None] = mapped_column(String(255), nullable=True)
    phone: Mapped[str | None] = mapped_column(String(50), nullable=True)
    role: Mapped[str] = mapped_column(String(100), nullable=False)
    preferred_channel: Mapped[PreferredChannel] = mapped_column(
        SQLEnum(PreferredChannel), nullable=False, default=PreferredChannel.PHONE
    )

    # Foreign keys
    lead_id: Mapped[int] = mapped_column(ForeignKey("leads.id"), nullable=False, index=True)

    # Relationships
    lead: Mapped[Lead] = relationship("Lead", back_populates="contacts")
